package com.hangwei.paperless.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hangwei.paperless.R;
import com.hangwei.paperless.utils.NotFastClickListener;
import com.twelve.all.utils.LogUtil;

/**
 * Created by 张龙臣 on 2020/11/27.
 * pidding 8 每个格子左右5
 */

public class SubProgressTitle extends NotFastClickListener {

    //    private int mTextMarging;
    private Context mContext;
    private boolean mIsLotData;
    //    private int mTextPadding;
    private final LinearLayout mLlContent;
    private final TextView mTvTrans;

    public void setTrans(boolean visible) {
        mTvTrans.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    public int getStep() {
        return mStep;
    }

    private int mStep;
    //    private int mTextSize;


    public View getInflate() {
        return mInflate;
    }

    private final View mInflate;

    private String[] arr = {"基础信息", "工步", "批量零件记录单", "数据填报", "零件检查状态记录单", "衬套装配单", "衬套加工单"//, "转交"
    };

    public void setIsLotData(boolean isLotData) {
        mIsLotData = isLotData;
        mLlContent.getChildAt(4).setVisibility(View.GONE);
        mLlContent.getChildAt(5).setVisibility(View.GONE);
        mLlContent.getChildAt(6).setVisibility(View.GONE);
        if (mIsLotData) {
            mLlContent.getChildAt(2).setVisibility(View.VISIBLE);
            mLlContent.getChildAt(3).setVisibility(View.GONE);
        } else {
            mLlContent.getChildAt(2).setVisibility(View.GONE);
            mLlContent.getChildAt(3).setVisibility(View.VISIBLE);
        }
    }

    public void showSub(Integer billType) {
        switch (billType) {
            case 1:
                mLlContent.getChildAt(4).setVisibility(View.VISIBLE);
                mLlContent.getChildAt(5).setVisibility(View.GONE);
                mLlContent.getChildAt(6).setVisibility(View.GONE);
                break;
            case 2:
                mLlContent.getChildAt(4).setVisibility(View.GONE);
                mLlContent.getChildAt(5).setVisibility(View.VISIBLE);
                mLlContent.getChildAt(6).setVisibility(View.GONE);
                break;
            case 3:
                mLlContent.getChildAt(4).setVisibility(View.GONE);
                mLlContent.getChildAt(5).setVisibility(View.GONE);
                mLlContent.getChildAt(6).setVisibility(View.VISIBLE);
                break;
        }
    }

    public interface OnProgressItemClickListener {
        void onProgressItemClick(int index, boolean completed);
    }

    private OnProgressItemClickListener l;

    public void setL(OnProgressItemClickListener onProgressItemClickListener) {
        this.l = onProgressItemClickListener;
    }

    public SubProgressTitle(Context context) {
        mContext = context;
        mInflate = LayoutInflater.from(mContext).inflate(R.layout.title_sub_progress, null);
        mLlContent = mInflate.findViewById(R.id.llContent);
        mTvTrans = mInflate.findViewById(R.id.tvTrans);
        mTvTrans.setOnClickListener(v -> {
            if (l != null) l.onProgressItemClick(100, false);
        });
//        mTextSize = Density.sp2px(mContext, mContext.getResources().getDimension(R.dimen.SP_14));
//        mTextSize = Density.sp2px(mContext, 42);
//        mTextMarging = Density.dip2px(context, 5);
//        int screenWidth = Tools.getScreenSize()[0];
//        for (int i = 0; i < arr.length; i++) {
//            Paint paint = new Paint();
//            Rect rect = new Rect();
//            TextView textView = new TextView(context);
//            textView.setText(arr[i]);
//            paint.setTextSize(mTextSize);
//            paint.getTextBounds(arr[i], 0, arr[i].length(), rect);
//            //未知原因显示不下，添加一个字的宽度
//            int textWidth = rect.width();
//            screenWidth -= textWidth;
//            screenWidth -= mTextMarging * 2;
//        }
//        mTextPadding = screenWidth / arr.length / 2;
        for (int i = 0; i < arr.length; i++) {
            mLlContent.addView(getText(i));
        }

//        mTvBaseInfo = mInflate.findViewById(R.id.tvBaseInfo);
//        mTvWorkStep = mInflate.findViewById(R.id.tvWorkStep);
//        mTvBatch = mInflate.findViewById(R.id.tvBatch);
//        mTvData = mInflate.findViewById(R.id.tvData);
//        mTvPartCheck = mInflate.findViewById(R.id.tvPartCheck);
//        mTvSetList = mInflate.findViewById(R.id.tvSetList);
//        mTvProcessList = mInflate.findViewById(R.id.tvProcessList);
//
//        setViewTag();
//        mTvBaseInfo.setOnClickListener(this);
//        mTvWorkStep.setOnClickListener(this);
//        mTvBatch.setOnClickListener(this);
//        mTvData.setOnClickListener(this);
//        mTvPartCheck.setOnClickListener(this);
//        mTvSetList.setOnClickListener(this);
//        mTvProcessList.setOnClickListener(this);
    }

    private View getText(final int index) {
//        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//        Paint paint = new Paint();
//        Rect rect = new Rect();
//        TextView textView = new TextView(mContext);
//        textView.setText(arr[index]);
//        paint.setTextSize(100);
//        paint.getTextBounds(arr[index], 0, arr[index].length(), rect);
//        //未知原因显示不下，添加一个字的宽度
//        int fixedPadding = Density.dip2px(mContext, 15);
//        params.width = rect.width() + fixedPadding * 2;
//        params.height = mTextSize + Density.dip2px(mContext, 8) * 2;
//        params.setMargins(mTextMarging, 0, mTextMarging, 0);
//        params.gravity = Gravity.CENTER;
//        textView.setLayoutParams(params);
//        LogUtil.e("fixedPadding------" + fixedPadding);
////        textView.setPadding(fixedPadding, Density.dip2px(mContext, 8), fixedPadding, Density.dip2px(mContext, 8));
//        textView.setGravity(Gravity.CENTER);

        View view = LayoutInflater.from(mContext).inflate(R.layout.title_text, mLlContent, false);
        TextView textView = view.findViewById(R.id.tvTitle);
        textView.setText(arr[index]);
//        textView.setTag(index);
        view.setOnClickListener(new com.twelve.all.utils.NotFastClickListener() {
            @Override
            protected void isNotFastClick(View v) {
                if (index == 4 || index == 5 || index == 6)
                    return;
                if (l != null) l.onProgressItemClick(index, false);
                setProgressStep(index);
            }
        });
        return view;
    }

    public void setProgressStep(int step) {
        mTvTrans.setVisibility(View.GONE);
        mStep = step;
        if (step > 6) {
            LogUtil.e("-------setProgressStep----------" + step);
        }
        if (step == 7)
            return;
//        if (step == 6) {
//            mLlContent.getChildAt(7).setVisibility(View.VISIBLE);
//        } else {
//            mLlContent.getChildAt(7).setVisibility(View.GONE);
//        }
        if (step == 3 || step == 0 || step == 1) {
            mLlContent.getChildAt(4).setVisibility(View.GONE);
            mLlContent.getChildAt(5).setVisibility(View.GONE);
            mLlContent.getChildAt(6).setVisibility(View.GONE);
        }
        int childCount = mLlContent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            TextView textView = (TextView) mLlContent.getChildAt(i).findViewById(R.id.tvTitle);
            if (step == i) {
                textView.setTextColor(mContext.getResources().getColor(R.color.white));
                textView.setBackgroundResource(R.drawable.sub_title_checked);
            } else {
                textView.setTextColor(mContext.getResources().getColor(R.color.blue3));
                textView.setBackgroundResource(R.drawable.sub_title_uncheck);
            }
//            textView.setPadding(mTextPadding, Density.dip2px(mContext, 12), mTextPadding, Density.dip2px(mContext, 12));
        }
    }
}
